package javaweb_work01.main.Servlet;

import javaweb_work01.main.Entity.Students;
import javaweb_work01.main.Service.CheckClassesIdservice;
import javaweb_work01.main.Service.CheckService;
import javaweb_work01.main.Service.CountService;
import javaweb_work01.main.Service.ShowService;
import javaweb_work01.main.Utils.PageContrl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 查找学员功能的Servlet类
 */
@WebServlet(name = "CheckServlet", urlPatterns = "/check")
public class CheckServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //得到mian.jsp页面中传过来的输入框的内容
        int checkNum = Integer.parseInt(request.getParameter("checkNum"));
        //调用查找方法得到Students对象的集合，即查找出来的学员数据
        CheckService checkService = new CheckService();
        List<Students> students= checkService.checkStudentsService(checkNum);
        //将学员数据集合作为值放入session共享
        request.getSession().setAttribute("targetStudent",students);

        //将查找的学员数据的数量作为分页显示的数据总数，构造对象用于控制分页
        PageContrl pageContrl = new PageContrl(0,10,students.size());

        //获取当前所有班级编号
        //调用Service层方法，进入Dao层并得到班级编号结果集
        CheckClassesIdservice checkClassesIdservice = new CheckClassesIdservice();
        List<Object[]> objects = checkClassesIdservice.checkClassesIdService();
        //准备一个集合用于存放结果集中的班级编号
        ArrayList<Integer> allClassesId = new ArrayList<>();
        for(Object[] j:objects){
            allClassesId.add((Integer) j[0]);
        }

        //将得到的PageContrl对象，Students对象，班级编号集合放入session中实现共享
        request.getSession().setAttribute("pageContrl",pageContrl);
        request.getSession().setAttribute("showStudents",students);
        request.getSession().setAttribute("allClassesId",allClassesId);
        //跳转到功能页面
        response.sendRedirect("main.jsp");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
